package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.web.service.ISmsSendDetailService;
import com.ruoyi.web.utils.UrlParamUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Twilio
 *
 * @since 2023-03-13
 */
@RestController
@RequestMapping("/twilio")
@AllArgsConstructor
public class TwilioSmsController extends BaseController {

    private final ISmsSendDetailService smsSendDetailService;

    /**
     * Twilio 消息状态变化通知
     * <p>成功消息
     * <p>SmsSid=SM95227b082071a5de0c13e215a340c601&SmsStatus=queued&MessageStatus=queued&To=%2B85255834429&MessagingServiceSid=MG7c2dfcfcee119e641bd7eb8639c68715&MessageSid=SM95227b082071a5de0c13e215a340c601&AccountSid=AC295b5b62ed7b69043e06bc29eedd1e46&From=%2B15856326946&ApiVersion=2010-04-01
     * <p>SmsSid=SM95227b082071a5de0c13e215a340c601&SmsStatus=sent&MessageStatus=sent&To=%2B85255834429&MessagingServiceSid=MG7c2dfcfcee119e641bd7eb8639c68715&MessageSid=SM95227b082071a5de0c13e215a340c601&AccountSid=AC295b5b62ed7b69043e06bc29eedd1e46&From=%2B15856326946&ApiVersion=2010-04-01
     * <p>RawDlrDoneDate=2304031137&SmsSid=SM95227b082071a5de0c13e215a340c601&SmsStatus=delivered&MessageStatus=delivered&To=%2B85255834429&MessagingServiceSid=MG7c2dfcfcee119e641bd7eb8639c68715&MessageSid=SM95227b082071a5de0c13e215a340c601&AccountSid=AC295b5b62ed7b69043e06bc29eedd1e46&From=%2B15856326946&ApiVersion=2010-04-01
     *
     * <p>失败消息
     * ErrorCode=21606&SmsSid=SM3cec7dadc3a14e024a1583b5563060ef&SmsStatus=failed&MessageStatus=failed&To=%2B85255834429&MessagingServiceSid=MG7c2dfcfcee119e641bd7eb8639c68715&MessageSid=SM3cec7dadc3a14e024a1583b5563060ef&AccountSid=AC295b5b62ed7b69043e06bc29eedd1e46&From=aaa&ApiVersion=2010-04-01
     *
     * @param str 通知
     * @since 2023/3/14 17:03
     */
    @Anonymous
    @PostMapping("/notify")
    public void statusNotify(@RequestBody String str) {
        Map<String, String> map = UrlParamUtil.getUrlParams(str);
        smsSendDetailService.statusNotify(map);
    }
}
